package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.MediaDAO;
import com.portalEsporteClubeBahia.app.vo.MediaVO;
import com.portalEsporteClubeBahia.app.vo.TypeMediaVO;

public class MediaBO {
	
	private MediaDAO mediaDAO;
	
	public MediaBO() throws ApplicationException{
		try {
			mediaDAO = new MediaDAO();
		} catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<MediaVO> findAll() throws ApplicationException{
		try{
			return mediaDAO.findAll();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<TypeMediaVO> findAllTypeMedia() throws ApplicationException{
		try{
			return mediaDAO.findAllTypeMedia();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void updateRanking(MediaVO media) throws ApplicationException{
		
		try{
			
			if(media.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			mediaDAO.updateRanking(media);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}

	}
	
	public List<MediaVO> findByFilter(MediaVO filter) throws ApplicationException{
		try{
			return mediaDAO.findByFilter(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public MediaVO findById(MediaVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return mediaDAO.findById(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<TypeMediaVO> findByFilter(TypeMediaVO filter) throws ApplicationException{
		try{
			return mediaDAO.findByFilter(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public TypeMediaVO findById(TypeMediaVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return mediaDAO.findById(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void delete(MediaVO media) throws ApplicationException{
		try{
			
			if(media.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			mediaDAO.delete(media);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void delete(TypeMediaVO type) throws ApplicationException{
		try{
			
			if(type.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			mediaDAO.delete(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void update(MediaVO media) throws ApplicationException{
		try{
			
			if(media.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			if(media.getTitle()==null || media.getTitle().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			if(media.getTypeMedia()==null || media.getTypeMedia().getId()==0){
				throw new ApplicationException("Missing Type.");
			}
			if(media.getTime()==null){
				throw new ApplicationException("Missing Time.");
			}
			if(media.getShortContent()==null || media.getShortContent().equals("")){
				throw new ApplicationException("Missing Short Content.");
			}
			if(media.getUrlMedia()==null || media.getUrlMedia().equals("")){
				throw new ApplicationException("Missing Url Image.");
			}
			
			mediaDAO.update(media);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void insert(MediaVO media) throws ApplicationException{
		try{
			
			if(media.getTitle()==null || media.getTitle().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			if(media.getTypeMedia()==null || media.getTypeMedia().getId()==0){
				throw new ApplicationException("Missing Type.");
			}
			if(media.getTime()==null){
				throw new ApplicationException("Missing Time.");
			}
			if(media.getShortContent()==null || media.getShortContent().equals("")){
				throw new ApplicationException("Missing Short Content.");
			}
			if(media.getUrlMedia()==null || media.getUrlMedia().equals("")){
				throw new ApplicationException("Missing Url Image.");
			}
			
			mediaDAO.insert(media);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void update(TypeMediaVO type) throws ApplicationException{
		try{
			
			if(type.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			if(type.getName()==null || type.getName().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			
			mediaDAO.update(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void insert(TypeMediaVO type) throws ApplicationException{
		try{
			
			if(type.getName()==null || type.getName().equals("")){
				throw new ApplicationException("Missing Name.");
			}
			
			mediaDAO.insert(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}

}
